package com.btpn.custody.web.m002.action;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;

import com.btpn.custody.bean.ValidationProduct;
import com.btpn.custody.common.Constant;
import com.btpn.custody.dao.IAddress;
import com.btpn.custody.dao.IMasterData;
import com.btpn.custody.entity.Address;
import com.btpn.custody.entity.MasterData;
import com.btpn.custody.web.m002.model.AllAddress;
import com.btpn.infra.utils.MyDateUtils;
import com.btpn.infra.utils.MyNumberUtils;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value="custody")
public class NonIndividuAction extends ActionSupport {

	/*=============================================== C O N S T A N T =========================================*/
	private static final long serialVersionUID = 390003476557787657L;
	
	/*=============================================== M E M B E R =============================================*/		
	@Autowired
	private IAddress addressDaoHibernate;
	
	@Autowired
	private IMasterData masterDataDaoHibernate;
		
	@Autowired
	private ValidationProduct validationProduct;
		
	private String masterSid;
//	private String CIF;
	private String CUST_FULL_NAME;				//*mandatory
	private String PARENT_COUNTRY;
	private String INFO_AKTE_PENDIRIAN;			//*
	private String INFO_AKTE_PENDIRIAN2;		//*
	private String AKTE_TERAKHIR_TANGGAL1;		//*
	private String AKTE_TERAKHIR_TANGGAL2;		//*
	private String SIUP;						//*
	private String TDP;
	private String TGL_AKHIR_TDP;				//*
	private String NPWP;
	private String CURRENT_INCOME;				//*
	private String SOURCE_INCOME;				//*
	private String ADDITIONAL_INCOME;
	private String PURPOSE;						//*
	private String JENIS_USAHA;
	private String LBU_GOL_NASABAH;				//*
	private String FREQ_TRX_DB;					//*
	private String FREQ_TRX_CR;					//*
	private String EST_TRX_NORMAL_DB;			//*
	private String EST_TRX_NORMAL_CR;			//*	
	private String NOREK_BANK_LAIN1;
	private String NOREK_BANK_LAIN2;
	private String NOREK_BANK_LAIN3;
	private String HR_CUSTOMER;
	private String HR_BUSINESS;
	private String HR_COUNTRIES;
	private String HR_PRODUCT;
	private String LAP_KEUANGAN;				//*
	private String HUB_DENGAN_BANK;				//*
	
	private AllAddress destAddress;
	
	/*=============================================== B E H A V I O R =========================================*/
	/*
	 * validate a product non_individu
	 * next : make object(product) to get the value, then move this bussiness to service
	 */
	@Action(value = "/validateNonIndividu", results = { 
		@Result(name="success", type="redirectAction", params = {"actionName", "checklist"} )
	})
	public String validateNonIndividu() {

		// get MasterData & update
		MasterData md = masterDataDaoHibernate.get(masterSid);
		md.setUpdatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
		md.setUpdatedDate(new Date());
//		masterDataDaoHibernate.update(md);
		
//		Long cifId = md.getCIF().getId();
		
		// update CIF
//		CIF cif = cifDaoHibernate.get(cifId);
		md.getCIF().setCUST_FULL_NAME(CUST_FULL_NAME);
		md.getCIF().setPARENT_COUNTRY(PARENT_COUNTRY);
		md.getCIF().setINFO_AKTE_PENDIRIAN(INFO_AKTE_PENDIRIAN);
		md.getCIF().setINFO_AKTE_PENDIRIAN2(INFO_AKTE_PENDIRIAN2);
		md.getCIF().setAKTE_TERAKHIR_TANGGAL1(AKTE_TERAKHIR_TANGGAL1);
		md.getCIF().setAKTE_TERAKHIR_TANGGAL2(AKTE_TERAKHIR_TANGGAL2);
		md.getCIF().setSIUP(SIUP);
		md.getCIF().setTANDA_DAFTAR_PERUSAHAAN(TDP);
		md.getCIF().setTGL_AKHIR_TDP(MyDateUtils.strToDate(Constant.FORMAT_PRESENTATION, TGL_AKHIR_TDP));
		md.getCIF().setNPWP(NPWP);
		md.getCIF().setCURRENT_INCOME(CURRENT_INCOME);
		md.getCIF().setSOURCE_INCOME(SOURCE_INCOME);
		md.getCIF().setADDITIONAL_INCOME(ADDITIONAL_INCOME);
		md.getCIF().setPURPOSE(PURPOSE);
		md.getCIF().setJENIS_USAHA(JENIS_USAHA);
		md.getCIF().setLBU_GOL_NASABAH(LBU_GOL_NASABAH);
		md.getCIF().setFREQ_TRX_DB(MyNumberUtils.decideNumeric(FREQ_TRX_DB));
		md.getCIF().setFREQ_TRX_CR(MyNumberUtils.decideNumeric(FREQ_TRX_CR));
		md.getCIF().setEST_TRX_NORMAL_DB(MyNumberUtils.decideNumeric(EST_TRX_NORMAL_DB));
		md.getCIF().setEST_TRX_NORMAL_CR(MyNumberUtils.decideNumeric(EST_TRX_NORMAL_CR));
		md.getCIF().setREK_BANK_LAIN1(NOREK_BANK_LAIN1);
		md.getCIF().setREK_BANK_LAIN2(NOREK_BANK_LAIN2);
		md.getCIF().setREK_BANK_LAIN3(NOREK_BANK_LAIN3);
		md.getCIF().setHR_CUSTOMER(HR_CUSTOMER);
		md.getCIF().setHR_BUSINESS(HR_BUSINESS);
		md.getCIF().setHR_COUNTRIES(HR_COUNTRIES);
		md.getCIF().setHR_PRODUCT(HR_PRODUCT);
		md.getCIF().setLAP_KEUANGAN(LAP_KEUANGAN);
		md.getCIF().setHUB_DENGAN_BANK(HUB_DENGAN_BANK);
//		cifDaoHibernate.update(cif);
		masterDataDaoHibernate.update(md);
		
		// validasi fisrt collumn
		if (StringUtils.trimToNull(destAddress.getAddress31()) != null) {
			Address a3 = addressDaoHibernate.getByMD_AddressType(md.getSid(), Address.PRIME_ADDRESS);
			if (a3==null) {
				a3 = new Address();
				a3.setCreatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
				a3.setCreatedDate(new Date());
				a3.setADDRESS_TYPE(Address.PRIME_ADDRESS);
				a3.setMasterData(md);
				addressDaoHibernate.create(a3);
			}
			a3.setADDRESS1(destAddress.getAddress31());
			a3.setADDRESS2(destAddress.getAddress32());
			a3.setADDRESS3(destAddress.getAddress33());
			a3.setADDRESS4(destAddress.getAddress34());
			a3.setADDRESS5(destAddress.getAddress35());
			a3.setPOST_CODE(destAddress.getPostCode3());
			a3.setLastUpdatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
			a3.setLastUpdatedDate(new Date());
			addressDaoHibernate.update(a3);
		}
		
		// validasi fisrt collumn
		if (StringUtils.trimToNull(destAddress.getAddress11()) != null) {
			Address a1 = addressDaoHibernate.getByMD_AddressType(md.getSid(), Address.HOME_ADDRESS);
			if (a1==null) {
				a1 = new Address();
				a1.setCreatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
				a1.setCreatedDate(new Date());
				a1.setADDRESS_TYPE(Address.HOME_ADDRESS);
				a1.setMasterData(md);
				addressDaoHibernate.create(a1);
			}
			a1.setADDRESS1(destAddress.getAddress11());
			a1.setADDRESS2(destAddress.getAddress12());
			a1.setADDRESS3(destAddress.getAddress13());
			a1.setADDRESS4(destAddress.getAddress14());
			a1.setADDRESS5(destAddress.getAddress15());
			a1.setPOST_CODE(destAddress.getPostCode1());
			a1.setLastUpdatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
			a1.setLastUpdatedDate(new Date());
			addressDaoHibernate.update(a1);
		}
		
		// validasi fisrt collumn
		if (StringUtils.trimToNull(destAddress.getAddress21()) != null) {
			Address a2 = addressDaoHibernate.getByMD_AddressType(md.getSid(), Address.OFFICE_ADDRESS);
			if (a2==null) {
				a2 = new Address();
				a2.setCreatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
				a2.setCreatedDate(new Date());
				a2.setADDRESS_TYPE(Address.OFFICE_ADDRESS);
				a2.setMasterData(md);
				addressDaoHibernate.create(a2);
			}
			a2.setADDRESS1(destAddress.getAddress21());
			a2.setADDRESS2(destAddress.getAddress22());
			a2.setADDRESS3(destAddress.getAddress23());
			a2.setADDRESS4(destAddress.getAddress24());
			a2.setADDRESS5(destAddress.getAddress25());
			a2.setPOST_CODE(destAddress.getPostCode2());
			a2.setLastUpdatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
			a2.setLastUpdatedDate(new Date());
			addressDaoHibernate.update(a2);	
		}
		
		// validasi fisrt collumn
		if (StringUtils.trimToNull(destAddress.getAddress81()) != null) {
			Address a8 = addressDaoHibernate.getByMD_AddressType(md.getSid(), Address.EMAILING_ADDRESS);
			if (a8==null) {
				a8 = new Address();
				a8.setCreatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
				a8.setCreatedDate(new Date());
				a8.setADDRESS_TYPE(Address.EMAILING_ADDRESS);
				a8.setMasterData(md);
				addressDaoHibernate.create(a8);
			}
			a8.setADDRESS1(destAddress.getAddress81());
			a8.setADDRESS2(destAddress.getAddress82());
			a8.setADDRESS3(destAddress.getAddress83());
			a8.setADDRESS4(destAddress.getAddress84());
			a8.setADDRESS5(destAddress.getAddress85());
			a8.setPOST_CODE(destAddress.getPostCode8());
			a8.setLastUpdatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
			a8.setLastUpdatedDate(new Date());
			addressDaoHibernate.update(a8);
		}
		
		// validate a product by CIF
		addActionMessage(validationProduct.validateProduct(md));
	
		return SUCCESS;
	}
	
	/*=============================================== G E T T E R   &   S E T T E R ===========================*/
	public String getMasterSid() {
		return masterSid;
	}

	public void setMasterSid(String masterSid) {
		this.masterSid = masterSid;
	}

	public String getCUST_FULL_NAME() {
		return CUST_FULL_NAME;
	}

	public void setCUST_FULL_NAME(String cUST_FULL_NAME) {
		CUST_FULL_NAME = cUST_FULL_NAME;
	}

	public String getPARENT_COUNTRY() {
		return PARENT_COUNTRY;
	}

	public void setPARENT_COUNTRY(String pARENT_COUNTRY) {
		PARENT_COUNTRY = pARENT_COUNTRY;
	}

	public String getINFO_AKTE_PENDIRIAN() {
		return INFO_AKTE_PENDIRIAN;
	}

	public void setINFO_AKTE_PENDIRIAN(String iNFO_AKTE_PENDIRIAN) {
		INFO_AKTE_PENDIRIAN = iNFO_AKTE_PENDIRIAN;
	}

	public String getINFO_AKTE_PENDIRIAN2() {
		return INFO_AKTE_PENDIRIAN2;
	}

	public void setINFO_AKTE_PENDIRIAN2(String iNFO_AKTE_PENDIRIAN2) {
		INFO_AKTE_PENDIRIAN2 = iNFO_AKTE_PENDIRIAN2;
	}

	public String getAKTE_TERAKHIR_TANGGAL1() {
		return AKTE_TERAKHIR_TANGGAL1;
	}

	public void setAKTE_TERAKHIR_TANGGAL1(String aKTE_TERAKHIR_TANGGAL1) {
		AKTE_TERAKHIR_TANGGAL1 = aKTE_TERAKHIR_TANGGAL1;
	}

	public String getAKTE_TERAKHIR_TANGGAL2() {
		return AKTE_TERAKHIR_TANGGAL2;
	}

	public void setAKTE_TERAKHIR_TANGGAL2(String aKTE_TERAKHIR_TANGGAL2) {
		AKTE_TERAKHIR_TANGGAL2 = aKTE_TERAKHIR_TANGGAL2;
	}

	public String getSIUP() {
		return SIUP;
	}

	public void setSIUP(String sIUP) {
		SIUP = sIUP;
	}

	public String getTDP() {
		return TDP;
	}

	public void setTDP(String tDP) {
		TDP = tDP;
	}

	public String getTGL_AKHIR_TDP() {
		return TGL_AKHIR_TDP;
	}

	public void setTGL_AKHIR_TDP(String tGL_AKHIR_TDP) {
		TGL_AKHIR_TDP = tGL_AKHIR_TDP;
	}

	public String getNPWP() {
		return NPWP;
	}

	public void setNPWP(String nPWP) {
		NPWP = nPWP;
	}

	public String getCURRENT_INCOME() {
		return CURRENT_INCOME;
	}

	public void setCURRENT_INCOME(String cURRENT_INCOME) {
		CURRENT_INCOME = cURRENT_INCOME;
	}

	public String getSOURCE_INCOME() {
		return SOURCE_INCOME;
	}

	public void setSOURCE_INCOME(String sOURCE_INCOME) {
		SOURCE_INCOME = sOURCE_INCOME;
	}

	public String getADDITIONAL_INCOME() {
		return ADDITIONAL_INCOME;
	}

	public void setADDITIONAL_INCOME(String aDDITIONAL_INCOME) {
		ADDITIONAL_INCOME = aDDITIONAL_INCOME;
	}

	public String getPURPOSE() {
		return PURPOSE;
	}

	public void setPURPOSE(String pURPOSE) {
		PURPOSE = pURPOSE;
	}

	public String getJENIS_USAHA() {
		return JENIS_USAHA;
	}

	public void setJENIS_USAHA(String jENIS_USAHA) {
		JENIS_USAHA = jENIS_USAHA;
	}

	public String getLBU_GOL_NASABAH() {
		return LBU_GOL_NASABAH;
	}

	public void setLBU_GOL_NASABAH(String lBU_GOL_NASABAH) {
		LBU_GOL_NASABAH = lBU_GOL_NASABAH;
	}

	public String getFREQ_TRX_DB() {
		return FREQ_TRX_DB;
	}

	public void setFREQ_TRX_DB(String fREQ_TRX_DB) {
		FREQ_TRX_DB = fREQ_TRX_DB;
	}

	public String getFREQ_TRX_CR() {
		return FREQ_TRX_CR;
	}

	public void setFREQ_TRX_CR(String fREQ_TRX_CR) {
		FREQ_TRX_CR = fREQ_TRX_CR;
	}

	public String getEST_TRX_NORMAL_DB() {
		return EST_TRX_NORMAL_DB;
	}

	public void setEST_TRX_NORMAL_DB(String eST_TRX_NORMAL_DB) {
		EST_TRX_NORMAL_DB = eST_TRX_NORMAL_DB;
	}

	public String getEST_TRX_NORMAL_CR() {
		return EST_TRX_NORMAL_CR;
	}

	public void setEST_TRX_NORMAL_CR(String eST_TRX_NORMAL_CR) {
		EST_TRX_NORMAL_CR = eST_TRX_NORMAL_CR;
	}

	public String getNOREK_BANK_LAIN1() {
		return NOREK_BANK_LAIN1;
	}

	public void setNOREK_BANK_LAIN1(String nOREK_BANK_LAIN1) {
		NOREK_BANK_LAIN1 = nOREK_BANK_LAIN1;
	}

	public String getNOREK_BANK_LAIN2() {
		return NOREK_BANK_LAIN2;
	}

	public void setNOREK_BANK_LAIN2(String nOREK_BANK_LAIN2) {
		NOREK_BANK_LAIN2 = nOREK_BANK_LAIN2;
	}

	public String getNOREK_BANK_LAIN3() {
		return NOREK_BANK_LAIN3;
	}

	public void setNOREK_BANK_LAIN3(String nOREK_BANK_LAIN3) {
		NOREK_BANK_LAIN3 = nOREK_BANK_LAIN3;
	}

	public String getHR_CUSTOMER() {
		return HR_CUSTOMER;
	}

	public void setHR_CUSTOMER(String hR_CUSTOMER) {
		HR_CUSTOMER = hR_CUSTOMER;
	}

	public String getHR_BUSINESS() {
		return HR_BUSINESS;
	}

	public void setHR_BUSINESS(String hR_BUSINESS) {
		HR_BUSINESS = hR_BUSINESS;
	}

	public String getHR_COUNTRIES() {
		return HR_COUNTRIES;
	}

	public void setHR_COUNTRIES(String hR_COUNTRIES) {
		HR_COUNTRIES = hR_COUNTRIES;
	}

	public String getHR_PRODUCT() {
		return HR_PRODUCT;
	}

	public void setHR_PRODUCT(String hR_PRODUCT) {
		HR_PRODUCT = hR_PRODUCT;
	}

	public String getLAP_KEUANGAN() {
		return LAP_KEUANGAN;
	}

	public void setLAP_KEUANGAN(String lAP_KEUANGAN) {
		LAP_KEUANGAN = lAP_KEUANGAN;
	}

	public String getHUB_DENGAN_BANK() {
		return HUB_DENGAN_BANK;
	}

	public void setHUB_DENGAN_BANK(String hUB_DENGAN_BANK) {
		HUB_DENGAN_BANK = hUB_DENGAN_BANK;
	}

	public AllAddress getDestAddress() {
		return destAddress;
	}

	public void setDestAddress(AllAddress destAddress) {
		this.destAddress = destAddress;
	}
}